package com.gable.exhibition.thread;

import com.gable.common.util.StringUtil;
import com.gable.exhibition.dao.UserDao;
import com.gable.exhibition.model.User;
import com.gable.exhibition.service.DeviceService;
import com.gable.exhibition.util.HttpHelper;
import com.gable.exhibition.view.UserView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mj on 2018/2/26.
 * 发货短信通知
 */
public class EstimateSendSmsThread implements Runnable{
    Logger logger = LoggerFactory.getLogger(this.getClass());

    private String hospitalName;        //医院名称

    private String serviceProviderName;   //乙方名称

    private String contractNo;          //合同编号

    private Integer sendCount;          //发货数量

    private List<String> hospitalMobile;      //医院管理员电话

    private String projectMobile;       //工程师电话

    private String serviceName;     //服务商名称

    private String engineerName;        //工程师名称

    private String estimateInstallDate;

    private String sendSmsForHospitalUrl;   //无工程师推送给医院

    private String sendSmsForEngineerUrl;   //推送给工程师

    private String sendSmsForHospitalAndEngineerUrl;    //有工程师推送给医院

    public EstimateSendSmsThread(String hospitalName,String serviceProviderName,String contractNo,
                                 Integer sendCount,List<String> hospitalMobile,String projectMobile,
                                 String serviceName,String engineerName,String estimateInstallDate,
                                 String sendSmsForHospitalUrl,String sendSmsForEngineerUrl,
                                 String sendSmsForHospitalAndEngineerUrl){
        this.hospitalName = hospitalName;
        this.serviceProviderName = serviceProviderName;
        this.contractNo = contractNo;
        this.sendCount = sendCount;
        this.hospitalMobile = hospitalMobile;
        this.projectMobile = projectMobile;
        this.serviceName = serviceName;
        this.engineerName = engineerName;
        this.estimateInstallDate = estimateInstallDate;
        this.sendSmsForHospitalUrl = sendSmsForHospitalUrl;
        this.sendSmsForEngineerUrl = sendSmsForEngineerUrl;
        this.sendSmsForHospitalAndEngineerUrl = sendSmsForHospitalAndEngineerUrl;
    }
    @Override
    public void run() {
        //推送给医院
        try {
            StringBuilder sb = new StringBuilder();
            if(hospitalMobile != null && hospitalMobile.size() > 0){
                for(String phone : hospitalMobile){
                    if(StringUtil.isEmpty(phone))
                        continue;
                    sb.append(phone).append(",");
                }
                String hospitalMobile = sb.substring(0, sb.length() - 1);
                String message = serviceProviderName+"%&"+contractNo+"%&"+sendCount;
                String url = sendSmsForHospitalUrl;
                //有工程师
                if(!StringUtil.isEmpty(serviceName) && !StringUtil.isEmpty(engineerName)){
                    message +="%&"+serviceName+"%&"+engineerName+"%&"+estimateInstallDate;
                    url = sendSmsForHospitalAndEngineerUrl;
                }

                Map<String,String> hospitalMap = new HashMap<String,String>();
                hospitalMap.put("mobile",hospitalMobile);
                hospitalMap.put("message",message);
                String jsonResult = HttpHelper.postRequestResponseBodyAsString(url, hospitalMap);
                logger.info("发货推送给医院的短信合同编号："+contractNo+",短信发送结果:"+jsonResult);
            }else{
                logger.info("发货推送给医院的短信合同编号："+contractNo+",短信发送结果:医院管理员电话为空");
            }
        } catch (Exception e) {
            logger.info("发货推送给医院的短信合同编号："+contractNo+",短信发送失败:"+e);
        }
        //推送给工程师
        try {
            if(!StringUtil.isEmpty(projectMobile)){
                Map<String,String> engineerMap = new HashMap<String,String>();
                engineerMap.put("mobile",projectMobile);
                String message = hospitalName+"%&"+contractNo;
                engineerMap.put("message",message);
                String jsonResult = HttpHelper.postRequestResponseBodyAsString(sendSmsForEngineerUrl, engineerMap);
                logger.info("发货推送给工程师的短信合同编号："+contractNo+",短信发送结果:"+jsonResult);
            }else{
                logger.info("发货推送给工程师的短信合同编号："+contractNo+",短信发送结果:工程师电话为空");
            }
        } catch (Exception e) {
            logger.info("发货推送给工程师的短信合同编号："+contractNo+",短信发送失败:"+e);
        }
    }
}
